var guest_relations = ["Первая","Вторая","Третья","Четвертая","Пятая","Шестая","Друзья"];

function calc(){
	$("#guest_count").text($(".guest_names").length);
}

function update_numbers(){
	var i = 1;
	$(".groom-number").each(function(){
		$(this).text(i++);
	});
	
	i = 1;
	$(".bride-number").each(function(){
		$(this).text(i++);
	});
}

$(document).ready(function(jQuery){

	// switch
	$("#groom_switch_off").live("click",function(){
		$(".groom-guests").hide();
		$(".groom-add").hide();
		$(this).attr("id","groom_switch_on");
		$(this).find(".switcher")[0].html("+");
	});
	$("#groom_switch_on").live("click",function(){
		$(".groom-guests").show();
		$(".groom-add").show();
		$(this).attr("id","groom_switch_off");
		$(this).find(".switcher")[0].html("–");
	});
	$("#bride_switch_off").live("click",function(){
		$(".bride-guests").hide();
		$(".bride-add").hide();
		$(this).attr("id","bride_switch_on");
		$(this).find(".switcher")[0].html("+");
	});
	$("#bride_switch_on").live("click",function(){
		$(".bride-guests").show();
		$(".bride-add").show();
		$(this).attr("id","bride_switch_off");
		$(this).find(".switcher")[0].html("–");
	});

	$("#groom-add-button").click(function(){
		
		var number = $(".groom-guests").length + 1;

		var pattern = "<td class='td-center groom-number'>"+number+"</td><td class='names'><input type='text' class='guest_names'></td>";
			pattern += "<td class='relations td-center'><select class='guest_relation'>";
			console.log(guest_relations);
			for (var i = 0; i < guest_relations.length; i++) {
				pattern += "<option value='"+i+"'>"+guest_relations[i]+"</option>";
			}
			pattern += "</select></td>";
			pattern += "<td class='confirmed td-center'><input type='checkbox' class='guest_confirmed'></td>";
			pattern += "<td><input type='button' class='guest_delete' value='Удалить'></td>";
		
		$(this).parent().parent().before("<tr class='groom-guests'>"+pattern+"</td>");
		calc();
	});

	$("#bride-add-button").click(function(){
		var number = $(".bride-guests").length + 1;

		var pattern = "<td class='td-center bride-number'>"+number+"</td><td class='names'><input type='text' class='guest_names'></td>";
			pattern += "<td class='relations td-center'><select class='guest_relation'>";
			for (var i = 0; i < guest_relations.length; i++) {
				pattern += "<option value='"+i+"'>"+guest_relations[i]+"</option>";
			}
			pattern += "</select></td>";
			pattern += "<td class='confirmed td-center'><input type='checkbox' class='guest_confirmed'></td>";
			pattern += "<td><input type='button' class='guest_delete' value='Удалить'></td>";

		$(this).parent().parent().before("<tr class='bride-guests'>"+pattern+"</td>");
		calc();
	});

	$(".guest_delete").live("click",function(){
		$(this).parent().parent().remove();
		calc();
		update_numbers();
	});

	// save
	$('#guest-save').click(function(){

		var params = "";
		var i = 0;
		$(".guest_names").each(function(){
			params += "objects%5B"+i+"%5D%5Bname%5D="+encodeURIComponent($(this).val());
			params += "&objects%5B"+(i++)+"%5D%5Btype%5D=";
			params += ($(this).parent().parent().hasClass("groom-guests")) ? "0&" : "1&";
		});
		i = 0;
		$(".guest_relation").each(function(){
			params += "objects%5B"+(i++)+"%5D%5Brelation%5D="+$(this).val()+"&";
		});
		i = 0;
		$(".guest_confirmed").each(function(){
			params += "objects%5B"+(i++)+"%5D%5Bconfirmed%5D="+($(this).attr("checked") == "checked" ? "1" : "0")+"&";
		});
		
		$.ajax({
			url: '/modules/mod_guest_list/ajax.php',
			type: "post",
			data: params,
			success: function(data){
				if (data == "success") {
					$("#save_result").html("<p class='saveSuccess'>Успешно сохранено</p>");
				} else {
					$("#save_result").html("<p class='saveFail'>Не удалось сохранить</p>");
				}
			},
			error: function(){
				$("#save_result").html("<p class='saveFail'>Не удалось сохранить</p>");
			}
		});
	});
});